package cn.q3c.parse.impl.smzdm;


import net.sf.json.JSONArray;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.log4j.Logger;

import cn.keyvalue.ccf.common.http.NewRequestUtils;
import cn.keyvalue.ccf.common.http.RequestUtils;
import cn.keyvalue.ccf.common.object.DetailPage;
import cn.keyvalue.ccf.common.object.Hyperlink;
import cn.keyvalue.ccf.common.util.ObjectUtils;
import cn.q3c.parse.base.BaseProcess;
import cn.q3c.parse.commons.wordpress.Post;
import cn.q3c.parse.commons.wordpress.WordPressAPI;
import cn.q3c.parse.dao.object.Match;

/**
 * 什么值得买Wordpress处理类，慧慧也用这个
 * @author wangxinning
 */
public class SmzdmIndexWPProcessImpl implements BaseProcess{

	private static final Logger logger = Logger.getLogger(SmzdmIndexWPProcessImpl.class);
	
	//适配数据
	private Match[] matchs = null;
	private long updateMatchTimes = 0;
	

	public boolean process(Object obj) {

		DetailPage page = (DetailPage)obj;
		if( page != null )
		{
			Post wpPost = new Post();
			
			wpPost.setPost_title(page.getBodytitle());
			wpPost.setPost_status("publish");
			wpPost.setPost_type("zhekouinfo");
			wpPost.setPost_excerpt("");
			wpPost.setPost_content(page.getBody());
			
			//图片 json array格式
			if( !ObjectUtils.isEmptyList(page.getImages()) )
			{		
				String[] imagesrcs = null;
				for( int i = 0; i < page.getImages().size(); i++ )
				{
					imagesrcs = ArrayUtils.add(imagesrcs, page.getImages().get(i).getSrc());
					
				}
				wpPost.addCustomField("image1", JSONArray.fromObject(imagesrcs).toString());
			}
			
			String subTitle = StringUtils.substringBetween(page.getBodytitle(), "<!--b-->", "<!--/b-->");
			if( StringUtils.isNotBlank(subTitle) )
			{
				wpPost.addCustomField("sub_title", subTitle);
				
				String title = StringUtils.substringBefore(page.getBodytitle(), "<!--b-->");
				wpPost.setPost_title(title);
			}
			
			if( page.getNextHyperlink() != null )
			{
				String targetUrl = page.getNextHyperlink().getUrl();
				String trueUrl = page.getNextHyperlink().getUrl();
				
				try
				{
					//转换为真实链接
					trueUrl = NewRequestUtils.getTrueURL(targetUrl);
				}
				catch( Exception e )
				{
					
				}
				
				wpPost.addCustomField("out_url", trueUrl);
			}
			
			//body keywords
			String[] srczkinfotags = null;
			if( !ObjectUtils.isEmptyList(page.getCrumbs()) )//面包屑
			{
				for( Hyperlink hyperlink:page.getCrumbs() )
				{					
					if( !ArrayUtils.contains(srczkinfotags, hyperlink.getName()) )
					{
						srczkinfotags = ArrayUtils.add(srczkinfotags, hyperlink.getName());
					}
				}
			}
			if( !ObjectUtils.isEmptyList(page.getTags()) )//面包屑
			{
				for( Hyperlink hyperlink:page.getTags() )
				{					
					if( !ArrayUtils.contains(srczkinfotags, hyperlink.getName()) )
					{
						srczkinfotags = ArrayUtils.add(srczkinfotags, hyperlink.getName());
					}
				}
			}
			if( !ObjectUtils.isEmptyList(page.getTypes()) )//面包屑
			{
				for( Hyperlink hyperlink:page.getTypes() )
				{					
					if( !ArrayUtils.contains(srczkinfotags, hyperlink.getName()) )
					{
						srczkinfotags = ArrayUtils.add(srczkinfotags, hyperlink.getName());
					}
				}
			}
			if( ArrayUtils.isNotEmpty(srczkinfotags) )
			{
				wpPost.addTerm("srczkinfotag", srczkinfotags);
			}
			
			//根据已有标签推导分类，标签+++
			if( System.currentTimeMillis()-this.updateMatchTimes > DateUtils.MILLIS_PER_MINUTE*5 )
			{
				try
				{
					updateMatchs();
				}
				catch( Exception e )
				{
					logger.error("更新分类适配规则失败", e);
				}
			}
			
			
			if( ArrayUtils.isNotEmpty(matchs) )
			{
				//适配
				String[] zkinfo = null;
				for( Match m:matchs )
				{
					//计算分类
					if( StringUtils.equals( m.getMatchtype(), "zkinfo") )
					{
						String[] tempArray = null;
						if( StringUtils.isNotBlank(m.getKeyword1()) ) tempArray = ArrayUtils.add(tempArray, m.getKeyword1());
						if( StringUtils.isNotBlank(m.getKeyword2()) ) tempArray = ArrayUtils.add(tempArray, m.getKeyword2());
						if( StringUtils.isNotBlank(m.getKeyword3()) ) tempArray = ArrayUtils.add(tempArray, m.getKeyword3());
						if( StringUtils.isNotBlank(m.getKeyword4()) ) tempArray = ArrayUtils.add(tempArray, m.getKeyword4());
	
						for( String temp:tempArray )
						{
							if( !ArrayUtils.contains(srczkinfotags, temp) )
							{
								continue;//必须全包含
							}
							
							zkinfo = ArrayUtils.add(zkinfo, m.getResult());
						}
					}
					
					//过滤title中关键字
					if( StringUtils.equals( m.getMatchtype(), "title") )
					{
						if( StringUtils.equals(m.getResult(), "remove") )
						{
							if( StringUtils.isNotBlank(m.getKeyword1()) )
							{
								wpPost.setPost_title( StringUtils.remove( wpPost.getPost_title(), m.getKeyword1() ));
							}
							if( StringUtils.isNotBlank(m.getKeyword2()) )
							{
								wpPost.setPost_title( StringUtils.remove( wpPost.getPost_title(), m.getKeyword2() ));
							}
							if( StringUtils.isNotBlank(m.getKeyword3()) )
							{
								wpPost.setPost_title( StringUtils.remove( wpPost.getPost_title(), m.getKeyword3() ));
							}
							if( StringUtils.isNotBlank(m.getKeyword4()) )
							{
								wpPost.setPost_title( StringUtils.remove( wpPost.getPost_title(), m.getKeyword4() ));
							}
						}
					}
					
					
					//过滤content中关键字
					if( StringUtils.equals( m.getMatchtype(), "content") )
					{
						if( StringUtils.equals(m.getResult(), "remove") )
						{
							if( StringUtils.isNotBlank(m.getKeyword1()) )
							{
								wpPost.setPost_content( StringUtils.remove( wpPost.getPost_content(), m.getKeyword1() ));
							}
							if( StringUtils.isNotBlank(m.getKeyword2()) )
							{
								wpPost.setPost_content( StringUtils.remove( wpPost.getPost_content(), m.getKeyword2() ));
							}
							if( StringUtils.isNotBlank(m.getKeyword3()) )
							{
								wpPost.setPost_content( StringUtils.remove( wpPost.getPost_content(), m.getKeyword3() ));
							}
							if( StringUtils.isNotBlank(m.getKeyword4()) )
							{
								wpPost.setPost_content( StringUtils.remove( wpPost.getPost_content(), m.getKeyword4() ));
							}
						}
					}
				}
				
				if( ArrayUtils.isNotEmpty(zkinfo) )
				{
					wpPost.addTerm("zkinfo", zkinfo);
				}
			}			

			String xmlrpcurl = "http://www.zhekou001.com/xmlrpc.php";
			String username = "zhekou001";
			String password = "Px7zRJW2K6u5zwFx";
			try
			{
				//WordPressAPI.newPost(xmlrpcurl, username, password, wpPost);
			}
			catch( Exception e )
			{
				e.printStackTrace();
			}
		}
		
		return true;
	}
	
	
	private void updateMatchs() throws Exception
	{
		//zkinfo
		String url = "http://q3c.cn/proxy/match.do?matchtype=zkinfo";
		String str = RequestUtils.urlToStr(url, "utf-8");		
		Match[] matchArray = (Match[])JSONArray.toArray(JSONArray.fromObject(str), Match.class);		
		
		//title中需要删除的
		url = "http://q3c.cn/proxy/match.do?matchtype=title";
		str = RequestUtils.urlToStr(url, "utf-8");
		matchArray = (Match[])ArrayUtils.addAll(matchArray, (Match[])JSONArray.toArray(JSONArray.fromObject(str), Match.class));
		
		//content
		url = "http://q3c.cn/proxy/match.do?matchtype=content";
		str = RequestUtils.urlToStr(url, "utf-8");
		matchArray = (Match[])ArrayUtils.addAll(matchArray, (Match[])JSONArray.toArray(JSONArray.fromObject(str), Match.class));

		this.matchs = matchArray;
		updateMatchTimes = System.currentTimeMillis();
	}
	
	
	public static void main( String[] args ) throws Exception
	{
		String url = "http://q3c.cn/proxy/match.do?matchtype=zkinfo";
		String str = RequestUtils.urlToStr(url, "utf-8");
		
		Object obj = JSONArray.toArray(JSONArray.fromObject(str), Match.class);
		
		System.out.println( ((Match[])obj)[0] );
	}
}